package com.trkj.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyMain {
    public static void main(String[] args) {
        // 目标对象
       // TargetInterface target = new DeptService();
        final TargetInterface target = new EmpService();
        // 增强对象
        final Advice advice = new Advice();

        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                //目标对象类加载器
                target.getClass().getClassLoader(),
                // 目标对象相同的接口字节码对象数组
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        advice.before();        //前置增强
                        Object invoke = method.invoke(target, args);
                        advice.after();         //后置增强

                        return invoke;
                    }
                }
        );
        // 通过代理类执行目标类的方法
        proxy.coreWork();
    }
}
